<?php
class AdmintoolController extends D_Controller {
	private $_website_obj;
	private $_crawler_obj;
	
	function init() {
		$this->setLayout ('main', '' );
		$this->_website_obj = new Website();
		$this->_crawler_obj = new Crawler();
	}
	
	function indexAction(){
		//echo "hello";
		//return;
	}
	
	function autoAction(){
		//$url="http://vnexpress.net/gl/kinh-doanh/2012/04/hon-6-trieu-nguoi-duoc-tang-luong-tu-1-5/";
		//$content = D_Http::get($url);
		//$res1 = D_KwUtils::header_from_content($content);
		
		// var_dump($res1);
	}
	
	
	
	function listcrawlerAction(){
		$params = $this->_request->getParams();
		$crawlers = $this->_crawler_obj->find_all(array('finished_at' => 'null'));
		$this->view->crawlers = $crawlers;
		//list crawler finished
		$crawler_finished = $this->_crawler_obj->find_all(array('finished_at' => 'not null'));
		$this->view->crawler_finished = $crawler_finished;
		if($this->_request->isPost()){
			//insert 
			$data = array('name' => $params["name"], 'depth' => $params["depth"]);
			$crawler_id = $this->_crawler_obj->insert($data);
			//assign website for crawler
			$result = $this->_crawler_obj->assign_website($crawler_id, $params["websites"]);
			if($result){
				$messages["sucessful"] = "Create Crawler successful!"; 
			}
			$this->view->messages = $messages;
			$this->view->params = $params;
		}//End isPost
		
	}
	
	//tao crawler moi
	function createcrawlerAction(){
		$params = $this->_request->getParams();
		$websites = $this->_website_obj->find_all();
		$this->view->hash_websites = DModel::convert_To_Hash($websites, "id", "url");
		$messages = array('a'=>'b');
		if($this->_request->isPost()){
			//insert 
			$data = array('name' => $params["name"], 'depth' => $params["depth"]);
			$crawler_id = $this->_crawler_obj->insert($data);
			//assign website for crawler
			$result = $this->_crawler_obj->assign_website($crawler_id, $params["websites"]);
			if($result){
				$messages["sucessful"] = "Create Crawler successful!"; 
			}
			$this->view->messages = $messages;
			$this->view->params = $params;
		}//End isPost
	}

	function insertwebsiteAction(){
		$errors = array();
		//list website
		$website_obj = new Website();
		$this->view->list_website = $website_obj->fetchAll();
		
		if($this->_request->isPost()){
			$name=$this->_request->getPost('txtname');    		
    		$url=$this->_request->getPost('txturl');  
    		$method =  $this->_request->getPost('method');   		
    		if($name=="")
    		{
    			$errors[]="[Name] not empty";
    			
    		}
    		if($url=="")
    		{
    			$errors[]="[Url] not empty";
    			
    		}else {
    			$model_web=new Website();
    			$data=array(
    				'name'	=>$name,
    				'url'	=>$method.$url
    			);
    			$check = $model_web->fetchOne(array('url'=>$url));
    			if($check)
    				$errors[]="Exits";
    			else
    				$model_web->insert_website($data);
    				$this->_redirect('/admintool/insertwebsite');
			}
			
			$this->view->errors=$errors;
		}
	}
	//Delete website
	function deletewebsiteAction(){
		$id=(int)$this->_request->getParam('id',0);
		if(!$id>0){
			$this->_redirect('/');
		}else{
			$website=new Website();
			$website->deletewebsite($id);
			$this->_redirect('/admintool/insertwebsite');
		}
		
	}
    /* Process cate*/
	function insertcateAction(){
		//list websitesss
		$web=new Website();
		//$this->view->dataweb=$web->listwebsite();
		
		//list cate
		$cate=new Category();
		$this->view->datacate=$cate->listcate();
		
		//add cate
		$errors = array();
		if($this->_request->isPost()){
			$cate=$this->_request->getPost('txtname');
			$type=$this->_request->getPost('type');
       		if($cate=="")
    		{
    			$errors[]="[Category] not empty";
    			
    		}else 
    		{
    			$m_cate=new Category();
    			$data_cate=array(
    				'name'	=>$cate,
    				'type'	=>$type,
    			);
    			$check = $m_cate->fetchOne($data_cate);
    			if ($check){
    				$errors[]="Exist";
    			}else{
    				$m_cate->insert_cate($data_cate);
    				$this->_redirect('/admintool/insertcate');
    			}
    		}
		}
		$this->view->errors=$errors;
	}
    function deletecateAction(){

    }
	function insertkeywordAction(){
		$key=new Keyword();
		$d_key=$key->listkeyword();
		$this->view->datakey=$d_key;
		if($this->_request->isPost()){
			$txtkey=$this->_request->getPost('txtkey');
			if($key==""){
				$this->view->error="[Keyword] not empty";
			}else {
				$data=array(
					'name'	=>$txtkey
				);
				$key->insert_keyword($data);
				$this->_redirect('/admintool/insertkeyword');
				
			}
		}
	}
	function deletekeywordAction(){
		$id=(int)$this->_request->getParam('id',0);
		if(!$id>0){
			$this->_redirect('/');
		}else{
			$keyword=new Keyword();
			$keyword->deletekeyword($id);
			$this->_redirect('/admintool/insertkeyword');
		}
		
	}
	//=============================================
	//assign website to website
	function webtocateAction(){
		$web_cate=new CategoryWebsite();
		$this->view->list_cate_website = $web_cate->list_cate_web();
		
		
		$web_obj=new Website();
		$d_web=$web_obj->listwebsite();
		//$this->view->datakey=$d_key;
		//list cate
		$cate=new Category();
		$d_cate=$cate->listcate();
		$this->view->datacate=$d_cate;
		
		$this->view->dataweb=$d_web;
		
		$errors = array();
		if($this->_request->isPost()){
			$cate_op=$this->_request->getPost('cate');
			$web_op=$this->_request->getPost('website');
			$newweb=$this->_request->getPost('txtweb');
			$newurl=$this->_request->getPost('txturl');
			
			$data_web=array(
					'category_id'	=>$cate_op,
					'website_id'	=> ''
			);			
			if($web_op==0){
				if($newweb=="" || $newurl=="")
				{	
					$errors[]="[Name] and [URL] not empty";
				}else {
					//check website	
					$web_data = array(
									'url'	=>$newurl
								);
					$check=$web_obj->fetchOne($web_data);	
					
					if($check){  // truong hop website nhap vao da ton tai					
						$data_web['website_id']= $check->id;
						
					}else 
					{
						$web_data = array( 'name'	=>$newweb,
									'url'	=>$newurl
								);
						//insert new website
						$web_id = $web_obj->insert_website($web_data);
						//and asign to kc array
						$data_web['website_id']= $web_id;
					}
					
				}
			}else //truong hop chon website
			{
				$data_web['website_id']= $web_op;
			}
			
			//check if assigned
			$check_assign = $web_cate->fetchOne($data_web);
			if($check_assign):
				$errors[]="Assigned";
			else:
				//insert assign
				$web_cate->insertcate_web($data_web);
				$this->_redirect('/admintool/webtocate');
			endif;
			
			//put errors array to view
			$this->view->errors=$errors;
		}
		
	}
	
	//=============================================
	//assign keyword to website
	function keytocateAction(){
		// list assign 
		$key_cate=new Categorieskeywords();
		$this->view->list_cate_keyword = $key_cate->list_cate_keyword();
		
		
		$key_obj=new Keyword();
		$d_key=$key_obj->listkeyword();
		//$this->view->datakey=$d_key;
		//list cate
		$cate=new Category();
		$d_cate=$cate->listcate(1);
		$this->view->datacate=$d_cate;
		
		$this->view->datakey=$d_key;
		if($this->_request->isPost()){
			$cate_op=$this->_request->getPost('cate');
			$key_op=$this->_request->getPost('keyword');
			$newkey=$this->_request->getPost('txtkeyword');
			
			$data_key=array(
					'category_id'	=>$cate_op,
					'keyword_id'	=> ''
			);
			
			if($key_op==0){
				if($newkey=="")
				{
					$errors[]="[KeyWord] not empty";
					
				}else {
					//check keyword	
					$kw_data = array('name'=>$newkey);
					$check=$key_obj->fetchOne($kw_data);			
					
					if($check){							
						$data_key['keyword_id']= $check->id;
					}else 
					{
						//insert new keyword
						$kw_id = $key_obj->insert_keyword($kw_data);
						//and asign to kc array
						$data_key['keyword_id']= $kw_id;
					}
					
				}
			}else 
			{
				$data_key['keyword_id']= $key_op;
			}
			//check if assigned
			$check_assign = $key_cate->fetchOne($data_key);
			if($check_assign):
				$errors[]="assigned";
			else:
				//insert assign
				$key_cate->insertcate_key($data_key);
				$this->_redirect('/admintool/keytocate');
			endif;
			
			//put errors array to view
			$this->view->errors=$errors;
		}
		
	}
	
	
	
	//====================================
	//
	//=============================================
	//assign keyword to website
	function websitekeywordAction(){
		// list assign
		$web_kw_obj =new WebsiteKeyword();
		$this->view->list_web_keyword = $web_kw_obj->find_all();
	
	
		$key_obj=new Keyword();
		$d_key=$key_obj->listkeyword();
		$this->view->datakey=$d_key;
		//list cate
		$web_obj=new Website();
		$web_list=$web_obj->find_all();
		$this->view->dataweb=$web_list;
	
		$this->view->datakey=$d_key;
		if($this->_request->isPost()){
			$web_op=$this->_request->getPost('opt_web');
			$key_op=$this->_request->getPost('opt_key');
			$newkey=$this->_request->getPost('txtkey');
				
			$data_key=array(
					'website_id'	=>$web_op,
					'keyword_id'	=> 0
			);
				
			if($key_op==0){
				if($newkey=="")
				{
					$errors[]="[KeyWord] not empty";
						
				}else {
					//check keyword
					$kw_data = array('name'=>$newkey);
					$check=$key_obj->fetchOne($kw_data);
						
					if($check){
						$data_key['keyword_id']= $check->id;
					}else
					{
						//insert new keyword
						$kw_id = $key_obj->insert_keyword($kw_data);
						//and asign to kc array
						$data_key['keyword_id']= $kw_id;
					}
						
				}
			}else
			{
				$data_key['keyword_id']= $key_op;
			}
			//check if assigned
			$check_assign = $web_kw_obj->fetchOne($data_key);
			if($check_assign):
				$errors[]="assigned";
			else:
				//insert assign
				$web_kw_obj->insert($data_key);
				$this->_redirect('/admintool/websitekeyword');
			endif;
				
			//put errors array to view
			$this->view->errors=$errors;
		}
	
	}
	function deletewebsitekeywordAction(){
		$id=(int)$this->_request->getParam('id',0);
		if(!$id>0){
			$this->_redirect('/');
		}else{
			$web_cate=new WebsiteKeyword();
			$web_cate->deletewebkey($id);
			$this->_redirect('/admintool/websitekeyword');
		}
		
	}
	
	public function listcateAction(){
		$listcate=new Category();
		$this->view->listcate=$listcate->listcate();
		//list key

		//$l_cate_web=new CategoryWebsite();
		//$this->view->cateweb=$l_cate_web->listwebsite();
	}
	public function clickcateshowwebAction(){
		$listcate=new Category();
		$this->view->listcate=$listcate->listcate();		
		//list key
		$id=(int)$this->_request->getParam('id',0);
		$cate_web=new CategoryWebsite();
		$this->view->list_website=$cate_web->listwebsite($id);
		
	}
	public function clickwebshowkeywordAction(){
		$listwebsite=new Website();
		$this->view->list_website=$listwebsite->listwebsite();
		
		$id=(int)$this->_request->getParam('id',0);
		$key_web=new WebsiteKeyword();
		$this->view->list_keyword=$key_web->listkeyword($id);
		
	}
	
}